<template>
	<view class="intelligence">
		<!--表单-->
		<view class="top">
			
			<uni-forms  ref="form" :modelValue="formData" :rules="rules">
				<uni-forms-item label="学科:" name="grade">
					<!-- <select v-model="formData.subjectId"  class="select" :label-in-value="true"  @change="change" ref="addRequestState">
						<option v-for="(item,index) in grades" :key="index" :value = "item.id">{{item.name}}</option>
					</select> -->
					<zw-select class="select" v-model="formData.subjectId" :data="grades" labelKey="name" valKey="id"></zw-select>
				</uni-forms-item>
				<uni-forms-item label="单选题:" name="singleChoice">
					<uni-easyinput class="register-box-input num" placeholder="2" v-model.trim="formData.singleChoice"/>
				</uni-forms-item>
				<uni-forms-item label="难度：" class="difficulty" name="difficulty">
					<uni-rate class="rate" v-model="formData.difficult"  :margin="5" :touchable="false" activeColor="#B4FF00"  @change="onChange" :size="24"/>
				</uni-forms-item>
			</uni-forms>
			<button @click="submit">生成试卷</button>
		</view>
		<view class="bottom">
			<view class="title">训练记录</view>
			<template v-if="trainItemList.length != 0">
				<view class="train-item" v-for="item in trainItemList" :key="item.id" >
					<view>
						<text>{{item.name}}</text>
						<text class="btn" @click="start(item.id)">开始答题</text>
					</view>
					<text>学科：{{item.subjectName}}</text>
					<text>创建时间：{{item.createTime}}</text>
				</view>
			</template>
			<view class="train-item" v-else>暂无数据</view>
			<view class="footer" v-if="flag"> ---我是有底线的---</view>
		</view>
		
	</view>
</template>

<script>
	import hpyFormSelect from '@/components/uni_modules/hpy-form-select/components/hpy-form-select/hpy-form-select.vue'
	export default {
		data() {
			return {
				//表单数据
				formData:{
					singleChoice: 20, //单选题数量
					subjectId:null, //年级
					difficult:2
				},
				//校验规则
				rules: {
					//对username字段进行必填校验
					singleChoice : {
						rules: [{
							required: true,
							errorMessage: "请填写单选题数量！"
						}]
						
					},
					subjectId : {
						rules: [{
							required: true,
							errorMessage: "请选择年级！"
						}]
					}
					
				},
				grades:[],
				trainItemList:[],
				query:{
					pageIndex: 1,
					pageSize: 10,
					paperType: 5
				},
				flag: false
			}
		},
		
		onReachBottom(){
			console.log("触底了");
			if(this.total <= this.trainItemList.length){
				this.flag = true;
				return ;
			}
			
			this.query.pageIndex++;
			//获取试卷列表
			this.getExamPaperList();
		},
		//监听下拉刷新
		onPullDownRefresh(){
			console.log("下拉刷新了");
			//刷新数据
			this.query.pageIndex = 1;
			this.trainItemList = [];
			this.flag = false;
			 //重新获取数据
			setTimeout( ()=>{this.getExamPaperList( ()=> {uni.stopPullDownRefresh()})},1000);
			
		},
		onLoad:function(){
			//获取token
			let token = uni.getStorageSync("token");
			console.log(!token);
			if( !token){
				// uni.showToast({
				// 	title: "请先登录！！"
				// })
				uni.redirectTo ({
					url: "/pages/login/login"
				});
			}
			//获取学科列表
			this.getSubjectList();
			this.getExamPaperList();
		},
		components:{hpyFormSelect},
		methods: {
			async getSubjectList(){
				const resp = await this.$myRequest({
					url: "/api/student/education/subject/list"
				})
				if( resp.data.code == 200){
					this.grades = resp.data.obj;
				}
			},
			//查询试卷列表
			async getExamPaperList(callBack){
				const resp = await this.$myRequest({
					url: "/api/student/exam/paper/pageList",
					data: this.query
				});
				console.log(resp.data);
				this.total = resp.data.total;
				this.trainItemList = [...this.trainItemList,...resp.data.data];
				callBack && callBack(); //关闭下拉刷新
			},
			submit(){
				this.$refs.form.validate().then(async res => {
					if( this.formData.singleChoice == ""){
						uni.showToast({
						  title: "请填写单选题数量！",
						  icon: "none",
						  duration: 2000,
						});
						return;
					}
					if( this.formData.subjectId == ""){
						uni.showToast({
						  title: "请选择年级！",
						  icon: "none",
						  duration: 2000,
						});
						return;
					}
					//进行创建
					const resp = await this.$myRequest({
						url: "/api/student/exam/paper/build",
						method:"post",
						data: this.formData
					});
					
					if(resp.data.code == 200){
						uni.showToast({
							title: resp.data.message
						})
						this.trainItemList = []
						this.getExamPaperList(); //重新刷新试卷
					}else{
						uni.showToast({
							title: resp.data.message
						})
					}
				}).catch(err =>{
					console.log('表单错误信息：', err);
				})
				
			},
			change(data){
				let index = this.$refs.addRequestState.value
				console.log(this.formData.subjectId);
			},
			onChange(e) { //难度条
				console.log('rate发生改变:' + JSON.stringify(e))
				console.log(this.formData.difficult)
			},
			start(id){ //开始答题
				console.log("试卷id："+id);
				uni.navigateTo({
					url: "/pages/answer/answer?paperId="+id
				})
			}
		}
	}
</script>

<style lang="scss">
.intelligence{
	display: flex;
	justify-content: center;
	flex-direction: column;
	align-items: center;
	width:100%;
	box-sizing: border-box;
	margin-bottom: 20rpx;
	.top {
		width:94%;
		margin-top: 50rpx;
		background-color: #fff;
		margin-top: 36rpx;
		padding: 30rpx 30rpx;
		box-sizing: border-box;
		border-radius: 20rpx;
		.select{
			width:40%;
			min-height: 72rpx;
			background: #F8F8F8;
			margin-top: 6rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			border: 1rpx solid #c8c7cc;
			border-radius: 8rpx;
			padding-left: 10rpx;
		}
		.register-box-input{
			background-color:#F8F8F8 ;
		}
		.num{
			width: 25%;
		}
		.difficulty{
			display: flex;
			align-items: center;
			font-size: 28rpx;
			text{
				margin-right: 20rpx;
			}
			margin-bottom: 20rpx;
			.rate{
				margin-top: 10rpx;
			}
		}
		button{
			margin-top: 10rpx;
			background: #66BB6A;
		}
	}
	.bottom{
		width:94%;
		margin-top: 50rpx;
		background-color: #fff;
		margin-top: 36rpx;
		margin-bottom: 20rpx;
		padding: 30rpx 30rpx;
		box-sizing: border-box;
		border-radius: 20rpx;
		.title{
			color:#212121;
			font-size: 32rpx;
		}
		.train-item{
			display: flex;
			flex-direction: column;
			margin: 20rpx 0 0 10rpx;
			padding-bottom: 30rpx;
			border-bottom: 2rpx solid #F8F8F8;
			view{
				display: flex;
				justify-content: space-between;
				align-items: center;
				.btn{
					color:#66BB6A;
				}
			}
			text{
				color:#707070;
				font-size: 28rpx;
				margin-top: 10rpx;
			}
		}
		.train-item:last-child{
			border-bottom: none;
		}
		.footer{
			font-size: 28rpx;
			color:#BFBFBF;
			text-align: center;
		}
	}
}
</style>
